var toolbar = [ {
	text : '新增',
	iconCls : 'icon-add',
	handler : function() {
		Handler.addData()
	}
}, '-', {
	text : '删除',
	iconCls : 'icon-cut',
	handler : function() {
		Handler.deleteData()
	}
}, '-', {
	text : '编辑',
	iconCls : 'icon-save',
	handler : function() {
		Handler.editData()
	}
}, '-', {
	text : '查询',
	iconCls : 'icon-search',
	handler : function() {
		Handler.searchDataById()
	}
} ];
// 设置分页控件
$(document).ready(function() {
	var p = $('#tablelist').datagrid('getPager');
	p.pagination({
		pageSize : 10,// 每页显示的记录条数，默认为10
		pageList : [ 10, 20, 50 ],// 可以设置每页记录条数的列表
		beforePageText : '第',// 页数文本框前显示的汉字
		afterPageText : '页    共 {pages} 页',
		displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录'
	});
	initSearchProject();
});
var Handler = {
	deleteData : function() {
		var rowsChecked = $('#tablelist').datagrid('getChecked');
		$.messager.confirm('信息提示', '确认是否删除数据?', function(r) {
			if (r) {
				$.post("/softSubSystem/removeSoftSubSystem", {
					id : rowsChecked[0].id
				}, function(data, state) {
					if (state == "success" && data.status == 200) {
						$('#tablelist').datagrid('reload');
						$('#ff').form('clear');
					} else {
						$.messager.alert('失败信息', data.message, 'error');
					}
				}, "json");
			}
		});
	},
	resetFormData : function() {
		$('#ff').form('clear');
	},
	searchData : function() {
		var params = getFormData("ff");
		$('#tablelist').datagrid('load', params);
	},
	searchDataById : function() {
		var rowsChecked = $('#tablelist').datagrid('getChecked');
		var url = "/softSubSystem/" + rowsChecked[0].id
				+ "/searchSoftSubSystem";
		$.get(url, function(data) {
			if (data.status == 200) {
				$('#infoForm').form('load', data.data);
				$("#dlg-buttons2").css('display', 'none');
			}
		}, "json");
		openWindow("formwin", "查询页面", "icon-search");
	},
	addData : function() {
		$('#infoForm').form('clear');
		$("#dlg-buttons2").css('display', 'block');
		initProjectInfo();
		openWindow("formwin", "新增页面", "icon-add");
	},
	editData : function() {
		$('#infoForm').form('clear');
		var rowsChecked = $('#tablelist').datagrid('getChecked');
		var url = "/softSubSystem/" + rowsChecked[0].id
				+ "/searchSoftSubSystem";
		$.get(url, function(data) {
			if (data.status == 200) {
				$('#infoForm').form('load', data.data);
				$("#dlg-buttons2").css('display', 'block');
			}
		}, "json");
		initProjectInfo();
		openWindow("formwin", "编辑页面", "icon-edit");
	},
	saveData : function() {
		var params = getFormData("infoForm");
		var url = "";
		if (params.id) {// 存在
			url = "/softSubSystem/editSoftSubSystem";
		} else {
			url = "/softSubSystem/addSoftSubSystem";
		}
		$.post(url, params, function(data, state) {
			if (state == "success" && data.status == 200) {
				$('#infoForm').form('clear');
				$('#formwin').window('close');
				var params = getFormData("ff");
				$('#tablelist').datagrid('load', params);
			} else {
				$.messager.alert('失败信息', data.message, 'error');
			}
		}, "json");
	}
};
function getFormData(id) {
	var data = $("#" + id).serializeArray();
	var item, key, value, newData = {};
	for (var i = 0; i < data.length; i++) {
		item = data[i];
		key = item.name;
		if (item.value) {
			value = item.value;
			newData[key] = value;
		}
	}
	return newData;
}

function openWindow(id, title, iconcls) {
	$('#' + id).window({
		width : 850,
		height : 400,
		top : ($("#body").height() - 400) * 0.5,
		left : ($("#body").width() - 800) * 0.5,
		shadow : true,
		collapsible : false,
		minimizable : false,
		maximizable : false,
		iconCls : iconcls,
		title : title,
		modal : true
	});
	$('#' + id).window('open');
}

function initProjectInfo() {

	$('#project_id').combobox({
		url : '/softProject/getSoftProjects',
		method : 'get',
		valueField : 'id',
		textField : 'text',
		panelHeight : 'auto',
		onSelect : function(param) {
			initSystemInfo(param.id);
		},
		onLoadSuccess : function(param) {
			if ($('#project_id').combobox('getValue') != "") {
				initSystemInfo($('#project_id').combobox('getValue'));
			}
		}
	});
}

function initSystemInfo(projectId) {
	$('#system_id').combobox({
		url : '/softSystem/getSoftSystems/' + projectId,
		method : 'get',
		valueField : 'id',
		textField : 'text',
		panelHeight : 'auto',
		onLoadSuccess : function(param) {
			if ($("#hiddenSystemId").val() != "") {
				$("#system_id").combobox('setValue',$("#hiddenSystemId").val());
				}
			$("#hiddenSystemId").val("");
		}
	});
}

function initSearchProject() {
	$('#searchProjectId').combobox({
		url : '/softProject/getSoftProjects',
		method : 'get',
		valueField : 'id',
		textField : 'text',
		panelHeight : 'auto',
		onSelect : function(param) {
			initSearchSystem(param.id);
		}
	});
}

function initSearchSystem(projectId) {
	$('#searchSystemId').combobox({
		url : '/softSystem/getSoftSystems/' + projectId,
		method : 'get',
		valueField : 'id',
		textField : 'text',
		panelHeight : 'auto'
	});
}
